import router from '@/router'
import useUserStore from './store/modules/user'
import pinia from './store'
const userStore = useUserStore(pinia)

router.beforeEach(async (to: any, from: any, next: any) => {
  const token = userStore.token
  console.log('userStore: ', to)
  const username = userStore.username
  if (token) {
    console.log(111)
    if (to.path == '/login') {
      console.log(333)
      next()
    } else {
      if (username) {
        next()
      } else {
        try {
          await userStore.userInfo()
          next({ ...to })
        } catch (error) {
          // await userStore.userInfo()
          next({ path: '/login' })
        }
      }
    }
  } else {
    console.log(222)
    if (to.path == '/login') {
      next()
    } else {
      next({ path: '/login' })
    }
  }
})

// router.afterEach((form: any, to: any, next: any) => {
//   console.log(222)
// })
